package com.happyscan138.makefun.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.happyscan138.makefun.BaseAdapter.my_message;
import com.happyscan138.makefun.R;
import com.readystatesoftware.viewbadger.BadgeView;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/12/7.
 */

public class myMessageAdapter extends BaseAdapter {
    public myMessageAdapter(Context mContext, List<my_message> mdata) {
        this.mContext = mContext;
        this.mdata = mdata;
    }
    private Context mContext;
    private List<my_message> mdata;
    private my_message bean;



    @Override
    public int getCount() {
       // log.e("messagedatas",mdata.size()+"");
        return mdata.size();
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder_MyMessage viewHolder;
        bean=mdata.get(position);
        //判断converView是否为空
        if(view==null){
            viewHolder=new ViewHolder_MyMessage();
            view=View.inflate(mContext, R.layout.my_message_item,null);
            viewHolder.myMessage_head=(ImageView)view.findViewById(R.id.my_message_head);
            viewHolder.myMessage_name=(TextView) view.findViewById(R.id.my_message_name);
            viewHolder.myMessage_time=(TextView) view.findViewById(R.id.my_message_time);
    //        viewHolder.myMessage_text=(TextView) view.findViewById(R.id.my_message_text);
            //将viewHolder与converView绑定
            view.setTag(viewHolder);
        }else {
            //如果存在converView，取出保存在converView中的viewHolder
            viewHolder=(ViewHolder_MyMessage) view.getTag();
        }

        //通过viewHolder找到对应控件，避免每次通过findViewById找控件
        log.e("messagedata",bean.head);

        Picasso.with(mContext).load(bean.head).into(viewHolder.myMessage_head);
        if(bean.is_read==0){
            BadgeView badgeView=new BadgeView(mContext, viewHolder.myMessage_head);
            badgeView.setText("");
            badgeView.setHeight(25);
            badgeView.setWidth(25);
            badgeView.setTextColor(Color.WHITE);
            badgeView.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
            badgeView.setAlpha(1f);
            badgeView.setBadgeMargin(0,0);
            badgeView.show();
        }
        viewHolder.myMessage_name.setText(bean.name);
        viewHolder.myMessage_time.setText(bean.time);
  //    viewHolder.myMessage_text.setText(bean.text);
        return view;
    }
    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }
}
class ViewHolder_MyMessage{
    public ImageView myMessage_head;
    public TextView myMessage_name;
    public TextView myMessage_time;
//    public TextView myMessage_text;
}